package cn.doitedu.hbase.bloomfilter

import java.io.{FileInputStream, FileOutputStream, ObjectInputStream, ObjectOutputStream}

import com.google.common.hash.{BloomFilter, Funnels}


/**
  * @date: 2019/8/18
  * @site: www.doitedu.cn
  * @author: hunter.d 涛哥
  * @qq: 657270652
  * @description: google布隆过滤器框架应用示例代码
  */
object GuavaBloomFilter {

  def main(args: Array[String]): Unit = {
    // genBloomFilter

    val filter: BloomFilter[CharSequence] = loadBloomFilter()

    println(filter.mightContain("aaa"))
    println(filter.mightContain("bbb"))
    println(filter.mightContain("abcd"))
    println(filter.mightContain("333"))

  }


  /**
    * 生成布隆过滤器，并记录数据，最后持久化保存
    */
  def genBloomFilter: Unit = {

    val bloomFilter: BloomFilter[CharSequence] = BloomFilter.create(Funnels.stringFunnel(), 10000, 0.001)

    bloomFilter.put("abc")
    bloomFilter.put("abcd")
    bloomFilter.put("44abc")
    bloomFilter.put("123")
    bloomFilter.put("333")

    // 判断一个字符串是否存在过
    println(bloomFilter.mightContain("444"))
    println(bloomFilter.mightContain("333"))
    println(bloomFilter.mightContain("abc"))
    println(bloomFilter.mightContain("abcd"))
    println(bloomFilter.mightContain("abcde"))

    // 布隆过滤器的持久化存储
    val out = new FileOutputStream("d:/bloom.obj")
    val objout = new ObjectOutputStream(out)

    objout.writeObject(bloomFilter)

    objout.close()
    out.close()

  }


  /**
    *  加载之前持久化保存好的布隆过滤器
    * @return
    */
  def loadBloomFilter(): BloomFilter[CharSequence] = {

    val in = new FileInputStream("d:/bloom.obj")
    val objIn = new ObjectInputStream(in)

    val bloomFilter = objIn.readObject().asInstanceOf[BloomFilter[CharSequence]]
    bloomFilter
  }


}
